// nxsnake == NorbiX's Snake
// Author: NorbiX
// MSN: bobcat01@hotmail.com
// e-mail: xibron[at]gmail[dot]com
#include <stdio.h>
#include <conio.h>
#include <windows.h>
void start_game(void);
char **build_matrix(int x, int y);
void fill_matrix(char **matrix, int x, int y, char c);
void print_matrix(char **matrix, int x, int y);
void get_matrix_size(char **matrix, int *x, int *y);
void get_console_size(int *x, int *y);
void clear(void);
char *fast_clear_init(void);
void fast_clear(char *text);
void gotoXY(int x, int y);
// eliminare get_matrix_size(), dato che non è possibile implementarla
int main()
{
int x, y;
char **matrix;
get_console_size(&x, &y);
printf("console size: %dx%d\n", x, y);
getch();
matrix = build_matrix(x, y);
fill_matrix(matrix, x, y, 'X');
//print_matrix(matrix, x, y);
printf("matrix size: %dx%d\n", x, y);
getch();
//start_game();
}
void start_game(void)
{
/* int x, y;
char **matrix;
get_console_size(&x, &y);
build_matrix(x, y);
fill_matrix(matrix, x, y, 'X');
print_matrix(matrix, x, y);*/
}
char **build_matrix(int x, int y)
{
int i;
char **matrix = (char **)malloc(y);
if(matrix == NULL) return NULL;
for(i = 0; i < y; i++)
{
matrix[i] = (char *)malloc(x);
//printf("matrix[%d] allocated\n", i);
if(matrix[i] == NULL) return NULL;
}
return matrix;
}
void fill_matrix(char **matrix, int x, int y, char c)
{
int i;
for(i = 0; i < y; i++)
{
// riesce a scrivere solo le prime 10 righe, non oltre, perchè?
memset(matrix[i], c, x);
//puts(matrix[i]);
// riesce a leggere tutti le 25 righe, non oltre
printf("\rmatrix[%d] filled\n", i);
}
}
void print_matrix(char **matrix, int x, int y)
{
int i;
for(i = 0; i < y; i++)
{
puts(matrix[i]);
}
}
void get_matrix_size(char **matrix, int *x, int *y)
{
*y = strrchr(matrix[0], 0) - matrix[0];
}
void get_console_size(int *x, int *y)
{
CONSOLE_SCREEN_BUFFER_INFO c_info;
HANDLE h_stdout;
h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(h_stdout, &c_info);
*x = c_info.srWindow.Right + 1;
*y = c_info.srWindow.Bottom + 1;
}
// creare un fast_clear() che pulisce velocemente, gli si passa la dimensione della console
void clear(void)
{
CONSOLE_SCREEN_BUFFER_INFO c_info;
HANDLE h_stdout;
char *text;
h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(h_stdout, &c_info);
// printf("Screen size: %dx%d\n", c_info.dwSize.X, c_info.dwSize.Y);
// printf("Screen size MAX: %dx%d\n", c_info.dwMaximumWindowSize.X, c_info.dwMaximumWindowSize.Y);
// printf("Cursor position: %d, %d\n", c_info.dwCursorPosition.X, c_info.dwCursorPosition.Y);
// printf("Boh: left: %d, right: %d, top: %d, bottom: %d\n", c_info.srWindow.Left, c_info.srWindow.Right, c_info.srWindow.Top, c_info.srWindow.Bottom);
// getchar();
// posiziono il cursore sull'angolo nord ovest della console
gotoXY(0, 0);
// alloco uno spazio equivalente al prodotto di righe e colonne della console
text = (char *)malloc((c_info.srWindow.Right + 1) * (c_info.srWindow.Bottom + 1));
// riempio la stringa ottenuta con spazi bianchi
memset(text, ' ', (c_info.srWindow.Right + 1) * (c_info.srWindow.Bottom + 1));
// pulisco lo schermo inserendo spazi bianchi dappertutto
puts(text);
// libero la memoria precedentemente allocata
free(text);
// riposiziono il cursore sull'angolo nord ovest della console
gotoXY(0, 0);
}
char *fast_clear_init(void)
{
CONSOLE_SCREEN_BUFFER_INFO c_info;
HANDLE h_stdout;
char *text;
h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(h_stdout, &c_info);
text = (char *)malloc((c_info.srWindow.Right + 1) * (c_info.srWindow.Bottom + 1));
memset(text, ' ', (c_info.srWindow.Right + 1) * (c_info.srWindow.Bottom + 1));
return text;
}
void fast_clear(char *text)
{
gotoXY(0, 0);
puts(text);
gotoXY(0, 0);
}
void gotoXY(int x, int y)
{
HANDLE h_stdout;
COORD coord;
coord.X = x;
coord.Y = y;
h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h_stdout, coord);
}